<template>
    <div class="user">
        <h2>个人中心</h2>
        <el-form ref="form" :model="formUser" label-width="80px">
            <el-form-item label="头像">
                <div class="cover">
                    <i v-if="formUser.hard===''" class="el-icon-plus"></i>
                    <img v-else :src="formUser.hard" style="width: 100%">
                    <input id="img" type="file" @change="imgSel" accept="image/jpeg" class="file">
                </div>
            </el-form-item>
            <el-form-item label="昵称">
                <el-input v-model="formUser.petname" style="width: 45%;"></el-input>
            </el-form-item>
            <el-form-item label="用户名">
                <el-input disabled v-model="formUser.name" style="width: 45%;"></el-input>
            </el-form-item>
            <el-form-item label="用户类型">
                {{formUser.role=='user'?'普通用户':'管理员'}}
            </el-form-item>
            <el-form-item label="性别" prop="resource">
                <el-radio-group v-model="formUser.sex">
                    <el-radio label="男"></el-radio>
                    <el-radio label="女"></el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit">修改</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    import {Loading} from 'element-ui'
    import api from '@/api'

    export default {
        beforeRouteEnter(to, from, next) {
            if (sessionStorage.uid) {
                next()
            } else {
                next('/index')
            }
        },
        data() {
            return {
                formUser: {
                    uid: '',
                    petname: this.$store.state.user.petname, //昵称
                    name: this.$store.state.user.name, //姓名
                    sex: this.$store.state.user.sex, //性别
                    hard: this.$store.state.user.hard, //头像
                    role: this.$store.state.user.role, //角色
                },
            }
        },
        methods: {
            imgSel(e) {
                const el = e.target
                this.formUser.hard = URL.createObjectURL(el.files[0])
            },
            loadinfo() {
                const loading = Loading.service({text: '正在加载用户信息'});
                api.getUserInfo().then(({data: resp}) => {
                    if (resp.code === 200) {

                        this.formUser.uid = resp.data.uid
                        this.formUser.petname = resp.data.petname
                        this.formUser.name = resp.data.name
                        this.formUser.sex = resp.data.sex
                        this.formUser.hard = resp.data.hard
                        this.formUser.role = resp.data.role

                    }
                    loading.close();
                }).catch(() => {
                    loading.close();
                })
            },
            submit() {
                if (this.formUser.petname == '') {
                    this.$message({message: '昵称不能为空', type: 'warning'})
                    return
                }
                if (this.formUser.sex == '') {
                    this.$message({message: '性别不能为空', type: 'warning'})
                    return
                }
                const img = document.getElementById('img').files[0]
                if (!img) {
                    this.$message({message: '请重新选择头像', type: 'warning'})
                    return
                }
                const data = new FormData();
                data.append('hard', img)
                data.append('sex', this.formUser.sex)
                data.append('petname', this.formUser.petname)
                api.edituser(data).then(({data: resp}) => {
                    if (resp.code === 200) {
                        this.$message({message: '修改成功', type: 'success'})
                    } else {
                        this.$message({message: resp.message, type: 'warning'})
                    }
                }).catch(() => {
                    this.$message({message: '网络好像有点问题', type: 'error'})
                })
            }
        }, created() {
            this.loadinfo()
        }
    }
</script>
<style scoped>
    .cover {
        overflow: hidden;
        display: inline-block;
        width: 100px;
        height: 100px;
        border-radius: 10px;
        border: dashed 3px #16d1ff;
        color: #1a83ff;
        font-size: 60px;
        font-weight: 900;
        line-height: 100px;
        text-align: center;
        position: relative;

    }

    .file {
        left: 0;
        top: 0;
        opacity: 0;
        position: absolute;
        width: 100%;
        height: 100%;
    }

</style>